﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Barbar.Colonization.Model;

namespace Barbar.Colonization.WorldEditor {
  public class TerrainToolStrip : ToolStrip {
    private bool m_DisableCheckChanged;
    private ITileType m_ActiveTileType;

    public ITileType ActiveTileType {
      get { return m_ActiveTileType; }
    }

    public void SetRuleSet(IRuleSet ruleSet) {
      foreach (ITileType tileType in ruleSet.TileTypes) {
        ToolStripButton button = new ToolStripButton(tileType.Name.ToString(), tileType.Images.MergedImage);
        button.Tag = tileType;
        button.CheckOnClick = true;
        button.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
        button.CheckedChanged += new EventHandler(button_CheckedChanged);
        Items.Add(button);
      }
    }

    void button_CheckedChanged(object sender, EventArgs e) {
      if (m_DisableCheckChanged)
        return;
      
      ToolStripButton currentButton = (ToolStripButton)sender;
      if (currentButton.Checked)
        m_ActiveTileType = (ITileType)currentButton.Tag;
      else
        m_ActiveTileType = null;

      m_DisableCheckChanged = true;
      foreach (ToolStripItem item in Items) {
        ToolStripButton button = item as ToolStripButton;
        if (button != null && button != sender) {
          button.Checked = false;
        }
      }
      m_DisableCheckChanged = false;
    }
  }
}
